﻿@model Barry.BarbeSystem.Models.Member
@{
    ViewBag.Title = "会员结账";
}
@section header{
    <style type="text/css">
        .noneBackgroud
        {
            background: none;
        }
    </style>
    <script type="text/javascript">

        var money = 0;
        $(document).ready(function () {
            $("#btnAddDetails").click(function () {
                if ($("#BarberDetails").val() != null) {
                    var text = $("#BarberDetails").find("option:selected").text();
                    var value = $("#BarberDetails").val();
                    money += parseFloat(value);
                    changeValue();
                    $("#checkOutDetail").append('<div><span>' + text + '</span><span>' + value + '</span><a onclick="deleteCurrent(this);" href="javascript:void(0)">删除</a></div>');
                }

            });
            $("#Category").change(function () {

                var categoryId = $("#Category").val();
                $.ajax({
                    url: "/barberdetail/GetDetailByCategory",
                    type: "post",
                    dataType: "json",
                    data: { id: categoryId },
                    success: function (values) {
                        //alert(values[0].Id);
                        var html = "";
                        for (var i = 0; i < values.length; i++) {
                            var currentItem = values[i];
                            html += "<option value='" + currentItem.Money + "'>" + currentItem.Name + "</option>";
                        }
                        $("#BarberDetails").html(html);
                    }

                });

            });


            $("#btnAddDetails").click(function () {
                var text = $("#BarberDetails").text();
                var money = $("#BarberDetails").val();
                $("#checkOutDetail").append("");
            });

            $(".classCheckOut").click(function () {
                var id = $("#Id").val();


                var details = "";
                $("#checkOutDetail").find("div").each(function () {
                    details += ($(this).find("span:eq(0)").text() + ":" + $(this).find("span:eq(1)").text() + ";");
                });


                var consumeAmont = $("#inputActualMoney").val();

                var balence = $("#Balance").val();

                if (parseFloat(balence) < parseFloat(consumeAmont)) {
                    $.messager.show({
                        title: '提示',
                        msg: "余额不足！"
                    });
                    return;
                }


                $.ajax({
                    url: "/member/ConsumeInfo",
                    type: "post",
                    dataType: "json",
                    data: { peopleId: id, consumeMoney: consumeAmont, details: details },
                    success: function (result) {

                        if (result.success) {

                            $("#checkOutDetail").find("div").each(function () {
                                var name = $(this).find("span:eq(0)").text();

                                var value = $(this).find("span:eq(1)").text();
                                $("#printInfo").append("<tr><td>" + name + "</td><td>" + value + "</td></tr>");
                            });
                            $("#printInfo").append("<tr><td>总金额</td><td>" + $("#moneyCount").text() + "</td></tr>");
                            $("#printInfo").append("<tr><td>折扣</td><td>" + $("#discount").text() + "</td></tr>");
                            $("#printInfo").append("<tr><td>实付金额</td><td>" + $("#inputActualMoney").val() + "</td></tr>");
                            var currentBalance = parseFloat($("#Balance").val()) - parseFloat($("#actualMoney").text());
                            $("#printInfo").append("<tr><td>余额</td><td>" + currentBalance + "</td></tr>");

                            var bodyHtml = $("body").html();
                            var printHtml = $("#printInfo").parent().html();

                            $("body").html(printHtml);
                            $("body").addClass("noneBackgroud");
                            window.print(printHtml);
                            $("body").html(bodyHtml);
                            $("body").removeClass("noneBackgroud");

                            $("#actualMoney").text("0");
                            $("#inputActualMoney").val("0");



                            $("#checkOutDetail").html("");
                            $("#moneyCount").html("");
                            $.messager.show({
                                title: '提示',
                                msg: "结账成功！"
                            });



                        } else {
                            $.messager.show({
                                title: '提示',
                                msg: "结账失败，请重新尝试！"
                            });
                        }
                        // Initphoto();
                    }
                });



            });
        });

        function changeValue() {
            $("#moneyCount").text(money);
            var discount = $("#discount").text();
            var number = parseInt(money * parseFloat(discount) * 0.1);
            $("#actualMoney").text(number);
            $("#inputActualMoney").val(number);

        }

        function deleteCurrent(obj) {
            var m = $(obj).parent().find("span:eq(1)").text();

            money -= parseFloat(m);
            $(obj).parent().remove();
            changeValue();

        }
    </script>
}
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>会员结账</legend>
        @Html.HiddenFor(model => model.Id)
        @Html.HiddenFor(model => model.Enable)
        @Html.HiddenFor(model => model.Balance)
        <div class="editor-label">
            @Html.LabelFor(model => model.CardId)
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.CardId)
            @Html.ValidationMessageFor(model => model.CardId)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Balance)
        </div>
        <div>
            @Html.DisplayFor(model => model.Balance)
            @Html.ValidationMessageFor(model => model.Balance)
        </div>
        <div class="editor-label">
            <label>
                请在下面选择消费详情</label>
        </div>
        <div class="editor-field">
            @Html.DropDownList("Category")
            <select id="BarberDetails">
            </select>
            <a href="javascript:void(0)" id="btnAddDetails">添加</a>
        </div>
        <div class="editor-label">
            <label>
                消费详情</label>
        </div>
        <div class="editor-field sum" id="checkOutDetail">
        </div>
        <div class="editor-label">
            <label>
                结账信息</label>
        </div>
        <div class="editor-field sum">
            <div>
                <span>总金额(元)：</span><span id="moneyCount">0</span></div>
            <div>
                <span>折扣：</span><span id="discount"> @Html.DisplayFor(model => model.Discount)</span></div>
            <div>
                <span>结账金额：</span><span id="actualMoney"> 0</span>
            </div>
            <div>
                <span>实际结账金额：</span>
                <input type="text" id="inputActualMoney" value="0" />
            </div>
        </div>
        <p>
            @*<input type="submit" value="确认结账" />*@ @*   <input type="button" value="结账" id="btnCheckout"  class="classCheckOut"/>*@
            <input type="button" value="结账并打印" id="print" class="classCheckOut" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("返回", "Index")
</div>
<div style="display: none;">
    <table border="0" id="printInfo">
        <tr>
            <td style="width: 100px;">
                项目
            </td>
            <td style="width: 100px;">
                详细
            </td>
        </tr>
    </table>
</div>
